﻿using System.Collections.Generic;
using System.Linq;
using DTO;
using System;

namespace DAO
{
    public class TinNhanDAO
    {
        public static List<TinNhanDTO> getAll()
        {
            List<TinNhanDTO> result = new List<TinNhanDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINNHANs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                TinNhanDTO dto = new TinNhanDTO();

                dto.MaTinNhan = ds.MaTinNhan;
                dto.MaNguoiDung = ds.MaNguoiDung.Value;
                dto.MaNguoiNhanTin = ds.MaNguoiNhanTin.Value;
                dto.TieuDeTinNhan = ds.TieuDeTinNhan;
                dto.NoiDungTinNhan = ds.NoiDungTinNhan;
                dto.ThoiGianNhanTin = ds.ThoiGianNhanTin.Value;
                dto.Readed = ds.Readed.Value;

                result.Add(dto);
            }

            return result;
        }

        /// <summary>
        /// Lấy các tin nhắn của người nhận tin
        /// </summary>
        /// <param name="maNguoiDung"></param>
        /// <returns></returns>
        public static List<TinNhanDTO> LayTinNhanTheoNguoiNhan(int maNguoiDung)
        {
            List<TinNhanDTO> Result = new List<TinNhanDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINNHANs.Where(dm => dm.Deleted.Equals(0) && dm.MaNguoiNhanTin == maNguoiDung).ToList();

            foreach (var ds in query)
            {
                TinNhanDTO dto = new TinNhanDTO();

                dto.MaTinNhan = ds.MaTinNhan;
                dto.MaNguoiDung = ds.MaNguoiDung.Value;
                dto.MaNguoiNhanTin = ds.MaNguoiNhanTin.Value;
                dto.TieuDeTinNhan = ds.TieuDeTinNhan;
                dto.NoiDungTinNhan = ds.NoiDungTinNhan;
                dto.ThoiGianNhanTin = ds.ThoiGianNhanTin.Value;
                dto.Readed = ds.Readed.Value;

                Result.Add(dto);
            }

            //Sắp xếp
            for (int i = 0; i < Result.Count - 1; ++i)
            {
                for (int j = i + 1; j < Result.Count; ++j)
                {
                    if (Result[i].ThoiGianNhanTin < Result[j].ThoiGianNhanTin)
                    {
                        DateTime Temp = Result[i].ThoiGianNhanTin;
                        Result[i].ThoiGianNhanTin = Result[j].ThoiGianNhanTin;
                        Result[j].ThoiGianNhanTin = Temp;
                    }
                }
            }

            return Result;
        }

        public static int LaySoTinNhanChuaDoc(int maNguoiNhan)
        {            
            try
            {
                RaoVatDataContext dbRaoVat = new RaoVatDataContext();

                var query = dbRaoVat.TINNHANs.Where(dm => dm.Deleted.Equals(0) &&
                    dm.MaNguoiNhanTin == maNguoiNhan && dm.Readed == 0).ToList();

                return query.Count;   
            }
            catch (System.Exception ex)
            {
                return 0;
            }                     
        }

        public static TinNhanDTO getAlongCode(int maTinNhan)
        {
            TinNhanDTO result = new TinNhanDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINNHANs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaTinNhan == maTinNhan).Single();

            result.MaTinNhan = query.MaTinNhan;
            result.MaNguoiDung = query.MaNguoiDung.Value;
            result.MaNguoiNhanTin = query.MaNguoiNhanTin.Value;
            result.TieuDeTinNhan = query.TieuDeTinNhan;
            result.NoiDungTinNhan = query.NoiDungTinNhan;
            result.ThoiGianNhanTin = query.ThoiGianNhanTin.Value;
            result.Readed = query.Readed.Value;

            return result;
        }

        public static void Insert(TinNhanDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINNHAN tinnhan = new TINNHAN();
            tinnhan.MaNguoiDung = _insert.MaNguoiDung;
            tinnhan.MaNguoiNhanTin = _insert.MaNguoiNhanTin;
            tinnhan.TieuDeTinNhan = _insert.TieuDeTinNhan;
            tinnhan.NoiDungTinNhan = _insert.NoiDungTinNhan;
            tinnhan.ThoiGianNhanTin = _insert.ThoiGianNhanTin;
            tinnhan.Readed = _insert.Readed;
            tinnhan.Deleted = _insert.Deleted;

            dbRaoVat.TINNHANs.InsertOnSubmit(tinnhan);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maTinNhan)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINNHAN _delete = (from n in dbRaoVat.TINNHANs
                                 where n.MaTinNhan == maTinNhan && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(TinNhanDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINNHAN _update = (from n in dbRaoVat.TINNHANs
                               where n.MaTinNhan == dto.MaTinNhan && n.Deleted == 0
                               select n).Single();

            _update.MaNguoiDung = dto.MaNguoiDung;
            _update.MaNguoiNhanTin = dto.MaNguoiNhanTin;
            _update.TieuDeTinNhan = dto.TieuDeTinNhan;
            _update.NoiDungTinNhan = dto.NoiDungTinNhan;
            _update.ThoiGianNhanTin = dto.ThoiGianNhanTin;
            _update.Readed = dto.Readed;

            dbRaoVat.SubmitChanges();
        }
    }
}
